<?php
//
// +------------------------------------------------------------------------+
// | Gecko Framework                                                        |
// +------------------------------------------------------------------------+
//
require_once('Gecko/Form/Builder/Interface.php');

/**
 * A Form Builder from a XML Schema
 *
 * @package Gecko.Form.Builder;
 * @author Christopher Valderrama <valderrama.christopher@gmail.com>
 * @copyright Copyright (c) 2008
 * @version $Id$v1.0$ 2008
 * @access public
 **/
class Gecko_Form_Builder_XML implements Gecko_Form_Builder_Interface {
	/**
	 * The Field schema
	 *
	 * @var array
	 */
	private $schema;
	/**
	 * The Form Settings
	 *
	 * @var array
	 */
	private $settings;

	/**
	 * This builder takes a XML File and constructs a
	 * Gecko_Form
	 *
	 * The XML File is simple:
	 * <forms>
	 *      <form name="form_name">
	 *           <fields>
	 *               <field type="text" name="UserName" value="">
	 *                     <settings>
	 *                           <size>10</size>
	 *                     </settings>
	 *                     <validators>
	 *                            <NotEmpty />
	 *                     </validators>
	 *               </field>
	 *           </fields>
	 *      </form>
	 * </forms>
	 *
	 * @access public
	 * @throws Gecko_Form_Builder_Exception
	 **/
	public function __construct($xmlFile) {
		if( !is_readable( $xmlFile ) ) {
			throw new Gecko_Form_Builder_Exception( "Unable to read $xmlFile, please check your path and permissions" );
		}

		$parser = new simplexml_load_file( $xmlFile );
		$form = $parser->form;

		$settings['name'] = (string) $form['name'];
		$this->settings = $settings;

		$fields = array();
		foreach( $form->fields as $field ) {
			$type = utf8_decode( (string) $field['type'] );
			$name = utf8_decode( (string) $field['name'] );
			$value = utf8_decode( (string) $field['value'] );
			$settings = array();
			foreach( (array) $field->settings as $setting => $value ) {
				$settings[(string)$setting] = utf8_decode( (string) $value );
			}

			$fields[] = array( "type" => $type, "name" => $name, "value" => $value, "settings" => $settings, "validators" => $validators );
		}

		$this->schema = $fields;
	}

	public function getFieldsSchema() {

	}

	public function getFormSettings() {
		return $this->settings;
	}
}
?>
